

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;
using System.Drawing;
using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store;
using JTC.MiniCommerce.Store.Web.Controls;
using SubSonic;
using SubSonic.Utilities;

namespace JTC.MiniCommerce.Web.admin.controls.product {
  public partial class images : AdminControl {
  
    #region Member Variables

    private int productId = 0;
    private string view = string.Empty;
    
    #endregion
    
    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        productId = Utility.GetIntParameter("productId");
        view = Utility.GetParameter("view");
        if (view == "i") {
          SetImagesProperties();
          if (!Page.IsPostBack) {
            LoadProductImages();
          }
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(images).Name + ".Page_Load", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnSave control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnSave_Click(object sender, EventArgs e) {
      if(Page.IsValid) {
        try {
          string imageId = lblImageId.Text;
          Where where = new Where();
          where.ColumnName = Store.Image.Columns.ProductId;
          where.DbType = DbType.Int32;
          where.ParameterValue = productId;
          Query query = new Query(Store.Image.Schema);
          object strSortOrder = query.GetMax(Store.Image.Columns.SortOrder, where);
          int maxSortOrder = 0;
          int.TryParse(strSortOrder.ToString(), out maxSortOrder);
          Store.Image image;
          if(!string.IsNullOrEmpty(imageId)) {
            image = new Store.Image(imageId);
          }
          else {
            image = new Store.Image();
            image.SortOrder = maxSortOrder + 1;
          }
          image.ProductId = productId;
          image.ImageFile = txtImageFile.Text.Trim();
          image.Caption = txtImageCaption.Text.Trim();
          image.Save(WebUtility.GetUserName());
          Reset();
          LoadProductImages();
          base.MasterPage.MessageCenter.DisplaySuccessMessage(LocalizationUtility.GetText("lblProductImageSaved"));
        }
        catch(Exception ex) {
          Logger.Error(typeof(images).Name + ".btnSave_Click", ex);
          base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
        }
      }
    }

    /// <summary>
    /// Handles the Click event of the lbDelete control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
    protected void lbDelete_Click(object sender, CommandEventArgs e) {
      try {
        int imageId = 0;
        bool isParsed = int.TryParse(e.CommandArgument.ToString(), out imageId);
        if(isParsed) {
          Store.Image.Delete(imageId);
          LoadProductImages();
          base.MasterPage.MessageCenter.DisplaySuccessMessage(LocalizationUtility.GetText("lblProductImageDeleted"));
        }
      }
      catch(Exception ex) {
        Logger.Error(typeof(images).Name + ".lbDelete_Click", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the lbEdit control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
    protected void lbEdit_Click(object sender, CommandEventArgs e) {
      try {
        int imageId = 0;
        bool isParsed = int.TryParse(e.CommandArgument.ToString(), out imageId);
        if(isParsed) {
          Store.Image image = new Store.Image(imageId);
          lblImageId.Text = image.ImageId.ToString();
          txtImageFile.Text = image.ImageFile;
          txtImageCaption.Text = image.Caption;
        }
      }
      catch(Exception ex) {
        Logger.Error(typeof(images).Name + ".lbEdit_Click", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the ItemReorder event of the Items control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Items_ItemReorder(object sender, EventArgs e) {
      try {
        ImageButton theButton = sender as ImageButton;
        Query query = new Query(Store.Image.Schema).WHERE(Store.Image.Columns.ProductId, productId).ORDER_BY(Store.Image.Columns.SortOrder);
        Store.ImageCollection imageCollection = new Store.ImageController().FetchByQuery(query);
        if (imageCollection != null) {
          int imageId = 0;
          int.TryParse(theButton.CommandArgument.ToString(), out imageId);
          if (imageId > 0) {
            Store.Image imageMoved = imageCollection.Find(delegate(Store.Image image) {
              return image.ImageId == imageId;
            });
            int index = imageCollection.IndexOf(imageMoved);
            imageCollection.RemoveAt(index);
            if (theButton.CommandName.ToLower() == "up") {
              imageCollection.Insert(index - 1, imageMoved);
            }
            else if (theButton.CommandName.ToLower() == "down") {
              imageCollection.Insert(index + 1, imageMoved);
            }
            int i = 1;
            foreach (Store.Image image in imageCollection) {
              image.SortOrder = i++;
            }
            imageCollection.SaveAll(WebUtility.GetUserName());
            LoadProductImages();
          }
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(general).Name + ".Page_Load", ex);
        throw;
      }
    }
    
    #endregion
    
    #region Events

    /// <summary>
    /// Handles the ItemDataBound event of the dgProductImages control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.DataGridItemEventArgs"/> instance containing the event data.</param>
    void dgProductImages_ItemDataBound(object sender, DataGridItemEventArgs e) {
      if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        LinkButton editButton = e.Item.Cells[0].FindControl("lbEdit") as LinkButton;
        if(editButton != null) {
          editButton.Text = LocalizationUtility.GetText("lblEdit");
        }
        LinkButton deleteButton = e.Item.Cells[4].FindControl("lbDelete") as LinkButton;
        if(deleteButton != null) {
          deleteButton.Text = LocalizationUtility.GetText("lblDelete");
          deleteButton.Attributes.Add("onclick", "return confirm(\"" + LocalizationUtility.GetText("lblConfirmDelete") + "\");return false;");
        }

        System.Web.UI.WebControls.Image image = e.Item.Cells[3].FindControl("productImage") as System.Web.UI.WebControls.Image;
        if(image != null) {
          string mappedPath = HttpContext.Current.Server.MapPath(image.ImageUrl);
          if (File.Exists(mappedPath)) {
            System.Drawing.Image drawnImage = System.Drawing.Image.FromFile(mappedPath);
            //do a little simple scaling
            //landscape
            if (drawnImage.Width > drawnImage.Height) {
              if (drawnImage.Width > 90) {
                image.Width = 90;
                image.Height = drawnImage.Height * 90 / drawnImage.Width;
              }
            }
            else { //portrait
              if (drawnImage.Height > 90) {
                image.Height = 90;
                image.Width = drawnImage.Width * 90 / drawnImage.Height;
              }
            }
            HyperLink hoverlink = e.Item.Cells[3].FindControl("hlImage") as HyperLink;
            if (hoverlink != null) {
              LocalizationUtility.AddHoverHtml(hoverlink, string.Format("<img border=\"0\" src=\"{0}\"/>", Page.ResolveUrl(image.ImageUrl)));
            }
          }
        }
        e.Item.Attributes.Add("onmouseover", "this.className = \"over\";");
        e.Item.Attributes.Add("onmouseout", "this.className = this.className.replace(\"over\", \"\");");
      }
    }    
    
    #endregion
    
    #region Methods
    
    #region Private

    /// <summary>
    /// Sets the images properties.
    /// </summary>
    private void SetImagesProperties() {
      LocalizationUtility.AddLabelText(lblImageFile);
      LocalizationUtility.AddLabelText(lblImageCaption);
      LocalizationUtility.AddButtonText(btnSave);
      LocalizationUtility.AddPanelText(pnlProductImages);
      hlImageSelector.NavigateUrl = string.Format("~/admin/imageselector.aspx?view=p&controlId={0}", txtImageFile.ClientID);
      LocalizationUtility.AddHoverHelp(hlImageFileHelp);
      LocalizationUtility.AddHoverHelp(hlImageCaptionHelp);
    }

    /// <summary>
    /// Resets this instance.
    /// </summary>
    private void Reset() {
      txtImageFile.Text = string.Empty;
      txtImageCaption.Text = string.Empty;
    }

    /// <summary>
    /// Loads the product images.
    /// </summary>
    private void LoadProductImages() {
      Query query = new Query(Store.Image.Schema).WHERE(Store.Image.Columns.ProductId, productId).ORDER_BY(Store.Image.Columns.SortOrder);
      Store.ImageCollection imageCollection = new Store.ImageController().FetchByQuery(query);
      dgProductImages.DataSource = imageCollection;
      dgProductImages.ItemDataBound += new DataGridItemEventHandler(dgProductImages_ItemDataBound);
      dgProductImages.Columns[0].HeaderText = LocalizationUtility.GetText("hdrEdit");
      dgProductImages.Columns[1].HeaderText = LocalizationUtility.GetText("hdrMove");
      dgProductImages.Columns[2].HeaderText = LocalizationUtility.GetText("hdrSortOrder");
      dgProductImages.Columns[3].HeaderText = LocalizationUtility.GetText("hdrImage");
      dgProductImages.Columns[4].HeaderText = LocalizationUtility.GetText("hdrImageCaption");
      dgProductImages.Columns[5].HeaderText = LocalizationUtility.GetText("hdrDelete");
      dgProductImages.DataBind();
      ImageButton lbUp = dgProductImages.Items[0].Cells[0].FindControl("lbUp") as ImageButton;
      if(lbUp != null) {
        lbUp.Visible = false;
      }
      ImageButton lbDown = dgProductImages.Items[dgProductImages.Items.Count - 1].Cells[0].FindControl("lbDown") as ImageButton;
      if(lbDown != null) {
        lbDown.Visible = false;
      }
    }

    #endregion
    
    #endregion

  }
}